qx.Class.define("caraffa.forms.CajaControl",
{
	extend : qx.ui.form.ow.FormTab,

  construct : function ()
  {
  		this.base(arguments, "CajaControl", "Caja: Control ");
  			var fecha = new qx.util.format.DateFormat("MM/yy");
			var mesMostrar = fecha.format(new Date());
  			this._mesMostrar = mesMostrar;
    
    // RPC
    this.rpcCaja = new qx.io.remote.Rpc();
		this.rpcCaja.setTimeout(10000);
		this.rpcCaja.setUrl("services/");
		this.rpcCaja.setServiceName("Caja");

		try{
			this.resRubros = this.rpcCaja.callSync("getRubros");
		}
		catch(ex){
			alert("Error: " + ex);
		}	

			this.grp1 = new qx.ui.groupbox.GroupBox();
	  		this.grp1.setWidth(370);      
	  		this.grp1.setLayout(new qx.ui.layout.VBox(5));
	  		
	  		this.grp1Mes = new qx.ui.groupbox.GroupBox();
	  			this.grp1Mes.setLayout(new qx.ui.layout.Basic());
	  			
	  			this.txtMesMostrar = new qx.ui.form.ow.TextField("Mes a Mostrar: <small>(mm/aa)</small>");
	  			this.txtMesMostrar.getLabel().setRich(true);
	  			this.txtMesMostrar.getText().setWidth(80);
	  			this.txtMesMostrar.getText().setTabIndex(1);
	  			this.btnMostrar = new qx.ui.form.Button("Recargar","caraffa/16/actualizar.png");
	  			this.cmbClinica = new qx.ui.form.ow.ComboBox("Clinica:", [{value:"1", label:"Zipoli"}, {value:"2", label:"Avellaneda"}], true);
	  			this.btnMostrar.setTabIndex(2);

	  		this.grp1Mes.add(this.cmbClinica,{left:0, top:0});
	  		this.grp1Mes.add(this.txtMesMostrar,{left:0, top:40});	
	  		this.grp1Mes.add(this.btnMostrar,{left:210, top:39});
	  		
	  		
	  		
	  		this.grp1Monto = new qx.ui.groupbox.GroupBox();
	  			this.grp1Monto.setLayout(new qx.ui.layout.Basic());
	  			

	  			this.lblMesT = new qx.ui.basic.Label("<b><big> MES:</big></b>");
		  			this.lblMesT.setRich(true);
	  			this.lblMesV = new qx.ui.basic.Label("");
	  				this.lblMesV.setRich(true);
	  			
	  			this.lblIngresosT = new qx.ui.basic.Label("<b><big> Total de Ingresos:</big></b>");
		  			this.lblIngresosT.setRich(true);
	  			this.lblIngresosV = new qx.ui.basic.Label("");
	  				this.lblIngresosV.setRich(true);
	  				
	  			this.lblEgresosT = new qx.ui.basic.Label("<b><big> Total de Egresos:</big></b>");
		  			this.lblEgresosT.setRich(true);
	  			this.lblEgresosV = new qx.ui.basic.Label("");
	  				this.lblEgresosV.setRich(true);
	  			
	  			this.lblLineaResultado = new qx.ui.basic.Label("____________");
	  			
	  			this.lblMontoFinalT = new qx.ui.basic.Label("<b><big> Monto Final:</big></b>");
		  			this.lblMontoFinalT.setRich(true);
	  			this.lblMontoFinalV = new qx.ui.basic.Label("");
	  				this.lblMontoFinalV.setRich(true);
	
	  		this.grp1Monto.add(this.lblMesT,{left:10, top:10});
	  		this.grp1Monto.add(this.lblMesV,{left:200, top:10});	  				
	  		this.grp1Monto.add(this.lblIngresosT,{left:10, top:40});
	  		this.grp1Monto.add(this.lblIngresosV,{left:200, top:40});
	  		this.grp1Monto.add(this.lblEgresosT,{left:10, top:60});
	  		this.grp1Monto.add(this.lblEgresosV,{left:200, top:60});
	  		this.grp1Monto.add(this.lblLineaResultado,{left:195, top:64});
	  		this.grp1Monto.add(this.lblMontoFinalT,{left:10, top:80});
	  		this.grp1Monto.add(this.lblMontoFinalV,{left:200, top:80});
	  		
	  		this.grp1Botonera = new qx.ui.groupbox.GroupBox();
	  			this.grp1Botonera.setLayout(new qx.ui.layout.HBox(30).set({alignX:"center"}));
	  			
	  			this.btnImprimir = new qx.ui.form.Button("Emitir Listado","caraffa/22/impresora.png");
	  				this.btnImprimir.setWidth(200);
	  				this.btnImprimir.setTabIndex(3);
	  		this.grp1Botonera.add(this.btnImprimir);	
	  		
	  	this.grp1.add(this.grp1Mes);
	  	this.grp1.add(this.grp1Monto);
	  	this.grp1.add(this.grp1Botonera);		
		
  	this.grp3 = new qx.ui.groupbox.GroupBox();
  		this.grp3.setWidth(760);
  		this.grp3.setLayout(new qx.ui.layout.Basic());
  		this.txtBuscar = new qx.ui.form.TextField();
  		this.txtBuscar.setEnabled(false);
  		this.txtBuscar.setWidth(130);
  		this.cmbBuscarRubro = new qx.ui.form.ow.ComboFilter("Rubro:",this.resRubros);
  		this.cmbBuscarRubro.getLabel().setWidth(50);
  		this.btnBuscar = new qx.ui.form.Button("Buscar");
  		this.btnReset = new qx.ui.form.Button("Resetear");
  	this.grp3.add(this.cmbBuscarRubro,{left:0,top:0});
  	this.grp3.add(this.txtBuscar,{left:270,top:0});
  	this.grp3.add(this.btnBuscar,{left:420,top:0});
  	this.grp3.add(this.btnReset,{left:500,top:0});
  		
  	this.grp2 = new qx.ui.groupbox.GroupBox();
  		this.grp2.setWidth(760);
  		this.grp2.setLayout(new qx.ui.layout.VBox(10));
    	this.mdlCajaMensual = new qx.ui.table.model.Simple();
			this.mdlCajaMensual.setColumns(["Op.N°","Fecha","Rubro","Descripción", "Ingreso", "Egreso", "Usuario","Total"]);
				this.tblCajaMensual = new qx.ui.table.Table(this.mdlCajaMensual);
					this.tblCajaMensual.setColumnWidth(0, 50);
					this.tblCajaMensual.setColumnWidth(1, 80);
					this.tblCajaMensual.setColumnWidth(2, 130);
					this.tblCajaMensual.setColumnWidth(3, 240);
					this.tblCajaMensual.setColumnWidth(4, 70);
					this.tblCajaMensual.setColumnWidth(5, 70);
					this.tblCajaMensual.setColumnWidth(6, 80);
					this.tblCajaMensual.setColumnWidth(7, 70);
						this.tblCajaMensual.getTableColumnModel().setColumnVisible(6,false);
					this.tblCajaMensual.setShowCellFocusIndicator(false);
					this.tblCajaMensual.setWidth(6000);
					this.tblCajaMensual.setHeight(200);
		this.txaMostrarFull = new qx.ui.form.ow.TextArea("Detalle:");
			this.txaMostrarFull.getText().setHeight(150);
			this.txaMostrarFull.getText().setReadOnly(true);
					
	this.grp2.add(this.tblCajaMensual);
	this.grp2.add(this.txaMostrarFull);
		
	this.add(this.grp1,{left:10,top:0});
	this.add(this.grp3,{left:380,top:0});
	this.add(this.grp2,{left:380,top:60});
	this._attachEvents();
	
  },
  
/*************************************************************************************************************************

 *************************************************************************************************************************
 *MEMBERS MEMBERS MEMBERS MEMBERSMEMBERS MEMBERS MEMBERS MEMBERS MEMBERS MEMBERS MEMBERS MEMBERS MEMBERS MEMBERS MEMBERS**
 *************************************************************************************************************************
 *************************************************************************************************************************/
members : 
	{
		_attachEvents : function (){
			this.addListener('appear', this._initForm, this);
			this.tblCajaMensual.addListener('cellClick', this._tblCajaMensualClick, this);
			this.btnMostrar.addListener('execute', this._btnMostrarExecute, this);
			this.btnImprimir.addListener('execute', this._btnImprimirExecute, this);
			this.btnBuscar.addListener('execute', this._buscar, this);
			this.btnReset.addListener('execute', this._reset, this);
			this.cmbBuscarRubro.addListener('changeValue', this._rubroCambia, this);
		},
		_initForm : function(){
			this._tblCajaMensualLoad();
		},
		_btnMostrarExecute:function(){
			this._mesMostrar = this.txtMesMostrar.getValue();
			this._tblCajaMensualLoad();
		},
		_btnImprimirExecute:function(){
			var params = new Object();
				params.mesMostrar = this._mesMostrar;		
				params.cod_rubro = this.cmbBuscarRubro.getValue();
				params.buscar = this.txtBuscar.getValue();
				params.buscarRubro = this.cmbBuscarRubro.getValueDisplay();
				params.cod_clinica = this.cmbClinica.getValue();
			var impresion = this.rpcCaja.callSync("printListado", params);
			window.open(impresion);
		},
		_buscar:function(){
			if(this.txtBuscar != ""){
				this.btnImprimir.setLabel("LISTADO FILTRADO");
				this._tblCajaMensualLoad();				
			}
		},
		_reset:function() {
			this.txtBuscar.setValue("");
			this.cmbBuscarRubro.setValue("");
			this.btnImprimir.setLabel("LISTADO TOTAL");
			this._tblCajaMensualLoad();
			this.txtBuscar.setEnabled(false);
		},		
		_tblCajaMensualLoad : function(){
			try{
				var params = new Object();
					if(this.txtBuscar != ""){
						params.buscar = this.txtBuscar.getValue();
						params.cod_rubro = this.cmbBuscarRubro.getValue();
					}
				params.mesMostrar = this._mesMostrar;
				params.cod_clinica = this.cmbClinica.getValue();
				var result = this.rpcCaja.callSync("getOperaciones", params);
				
				if(result.ingresoTotal != ""  || this.txtBuscar != ""){
					this.mdlCajaMensual.setData(result.values);
					this.lblMesV.setValue("<b><big>"+result.mes+"</big></b>");
					this.lblIngresosV.setValue("<b><big><font color='green'>$"+result.ingresoTotal+"</font></big></b>");
					this.lblEgresosV.setValue("<b><big><font color='red'>$"+result.egresoTotal+"</font></big></b>");
					this.lblMontoFinalV.setValue("<b><big><font color='green'>$"+result.montoDisponible+"</font></big></b>");
				}
				else{
						alert("Mes aún no registrado o invalido (Formato: MM/AA)");
				}
			}
			catch(ex){
				alert("Error: " + ex);
			}	
		},
		_rubroCambia:function(){
			if(this.cmbBuscarRubro.getValueDisplay() == ""){
				this.txtBuscar.setEnabled(false);
			}
			else{
				this.txtBuscar.setEnabled(true);
			}
		},
		_tblCajaMensualClick:function(){
			var cod_operacion = this.mdlCajaMensual.getValue(0, this.tblCajaMensual.getFocusedRow());
			var fecha = this.mdlCajaMensual.getValue(1, this.tblCajaMensual.getFocusedRow());
			var usuario = this.mdlCajaMensual.getValue(6, this.tblCajaMensual.getFocusedRow());
			var valorIngreso = this.mdlCajaMensual.getValue(4, this.tblCajaMensual.getFocusedRow());
			var valorEgreso = this.mdlCajaMensual.getValue(5, this.tblCajaMensual.getFocusedRow());
			var rubro = this.mdlCajaMensual.getValue(2, this.tblCajaMensual.getFocusedRow());
			if(valorIngreso !=""){
				var tipo = "Ingreso";
				var importe = valorIngreso;
			}
			else if(valorEgreso !=""){
				var tipo = "Egreso";
				var importe = valorEgreso;				
			}
			var glosa = this.mdlCajaMensual.getValue(3, this.tblCajaMensual.getFocusedRow());
			var mostrar = "Operación N°: "+cod_operacion +"                Fecha: "+fecha+"                Rubro: "+rubro+"                Usuario: "+usuario;
				mostrar+="\n----------------------------------------------------------------------------------------------------------------------------------------------------------\n";
				mostrar+=tipo+": "+importe+"\n";
				mostrar+="Descripción: "+glosa;
			this.txaMostrarFull.setValue(mostrar);
		}
	}
});

    